<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of controller
 *
 * @author Christian Simon <christian.simon1988@gmail.com>
 */
class controllerCharacter {

    public static function get($int) {
        $sql = "SELECT * FROM  `character` where `_id` = " . $int;
        $query = MySql::query($sql);
        while ($row = mysql_fetch_object($query)) {
            $character = new modelCharacter();
            return $character->deserialize($row);
        }
    }
    
    public static function getCountByType($type)
    {
        $sql = "SELECT count(*) FROM  `character` WHERE  `type` =  '" . $type . "'";
        $query = MySql::query($sql);
        $row = mysql_fetch_row($query);
        return $row[0];
    }

    public static function getMainChars() {
        $sql = "SELECT * FROM `character` WHERE `_cid` IS NULL";
        $result = MySql::query($sql);
        while ($row = mysql_fetch_object($result)) {
            echo "<option value=" . $row->_id . ">" . htmlentities($row->surname, ENT_QUOTES, 'UTF-8') . "</option>";
        }
    }

    public static function update($character) {
        $character = MySql::removeNull($character);
        $sql = "UPDATE `vdl_roleplay`.`character` SET `_cid` = " .
                $character->_cid . ", `_oid` = " .
                $character->_oid . ", `_rid` = " .
                $character->_rid . ", `surname` = " .
                "\"" . $character->surname . "\", `lastname` = " .
                "\"" . $character->lastname . "\", `age` = " .
                $character->age . ", `gender` = " .
                "\"" . $character->gender . "\", `race` = " .
                "\"" . $character->race . "\", `portrait` = " .
                "\"" . $character->portrait . "\", `status` = " .
                "\"" . $character->status . "\", `profession` = " .
                "\"" . $character->profession . "\", `type` = " .
                "\"" . $character->type . "\", `task` = " .
                "\"" . $character->task . "\", `class` = " .
                "\"" . $character->class . "\", `shortdescription` = " .
                "\"" . $character->shortdescription . "\" WHERE `character`.`_id` = " . $character->_id . ";";
        MySql::query($sql);
        return mysql_affected_rows();
        //return $sql;
    }

    public static function add($character) {
        $character = MySql::removeNull($character);
        $sql = "INSERT INTO `vdl_roleplay`.`character` (`_id`, `_cid`, `_oid`, `_rid`, `surname`, `lastname`, `age`, `gender`, `race`, `portrait`, `status`, `profession`, `type`, `task`, `class`, `shortdescription`) VALUES (NULL, " .
                "\"" . $character->_cid . "\"," .
                "\"" . $character->_oid . "\"," .
                "\"" . $character->_rid . "\", " .
                "\"" . $character->surname . "\", " .
                "\"" . $character->lastname . "\", " .
                "\"" . $character->age . "\", " .
                "\"" . $character->gender . "\", " .
                "\"" . $character->race . "\", " .
                "\"" . $character->portrait . "\", " .
                "\"" . $character->status . "\", " .
                "\"" . $character->profession . "\", " .
                "\"" . $character->type . "\", " .
                "\"" . $character->task . "\", " .
                "\"" . $character->class . "\", " .
                "\"" . $character->shortdescription . "\");";
        MySql::insert($sql);
        return mysql_affected_rows();
    }

    public static function delete($int) {
        $sql = "DELETE FROM  `character` where `_id` = " . $int;
        MySql::query($sql);
        return mysql_affected_rows();
    }

    public static function getOrganisation($int) {
        if ($int != null) {
            $sql = "select `name` from organisation where `_id` = " . $int;
            $result = MySql::query($sql);
            $row = mysql_fetch_row($result);
            return htmlentities($row[0], ENT_QUOTES, 'UTF-8');
        }
    }

    public static function getCharacter($int) {
        if ($int != null) {
            $sql = "select `surname` from `character` where `_id` = " . $int;
            $result = MySql::query($sql);
            $row = mysql_fetch_row($result);
            return htmlentities($row[0], ENT_QUOTES, 'UTF-8');
        }
    }

    public static function getRank($int) {
        if ($int != null) {
            $sql = "select `name` from `rank` where `_id` = " . $int;
            $result = MySql::query($sql);
            if ($result != "null") {
                $row = mysql_fetch_row($result);
                return htmlentities($row[0], ENT_QUOTES, 'UTF-8');
            }
        }
    }

    public static function getRelations($int) {
        if ($int != null) {
            $sql = "SELECT *  FROM `relations` WHERE `_characterid` = " . $int;
            $result = MySql::query($sql);
            while ($row = mysql_fetch_object($result)) {
                echo self::getCharacter($row->_relationid) . " (" . $row->type . ")<br>";
            }
        }
    }

    public static function getGender($string)
    {
        echo "<img width='40' height='40' src='./res/layout/ic_gender_";
        echo $string;
        echo ".png' title='";
        echo $string;
        echo "'>";
    }
    
    /*
     * Table Headers
     */

    public static function getOverviewHeader() {
        echo "<th class='header headerSortDown'>Vorname</th>";
        echo "<th class='header'>Nachname</th>";
        echo "<th class='header'>Klasse</th>";
        echo "<th class='header'>Organisation</th>";
        echo "<th class='header'>Rasse</th>";
        echo "<th class='header'>Alter</th>";
        echo "<th class='header'>Status</th>";
        echo "<th class='header'>Herstellungsberuf</th>";
    }

    public static function getRoleplayHeader() {
        echo "<th class='header headerSortDown'>Vorname</th>";
        echo "<th class='header'>Nachname</th>";
        echo "<th class='header'>Organisation</th>";
        echo "<th class='header'>Rang</th>";
        echo "<th class='header'>Rasse</th>";
        echo "<th class='header'>Alter</th>";
        echo "<th class='header'>Status</th>";
        echo "<th class='header'>Beziehungen</th>";
    }

    public static function getGuildHeader() {
        echo "<th class='header headerSortDown'>Vorname</th>";
        echo "<th class='header'>Nachname</th>";
        echo "<th class='header'>Klasse</th>";
        echo "<th class='header'>Rang</th>";
        echo "<th class='header'>Rasse</th>";
        echo "<th class='header'>Gruppenfunktion</th>";
        echo "<th class='header'>Twink von</th>";
        echo "<th class='header'>Herstellungsberuf</th>";
    }

    public static function getNscHeader() {
        echo "<th class='header headerSortDown'>Vorname</th>";
        echo "<th class='header'>Nachname</th>";
        echo "<th class='header'>Gruppe</th>";
        echo "<th class='header'>Titel</th>";
        echo "<th class='header'>Rasse</th>";
        echo "<th class='header'>Alter</th>";
        echo "<th class='header'>Status</th>";
        echo "<th class='header'>Beschreibung</th>";
    }

    /*
     * Table Rows
     */

    public static function getOverviewRow($string) {
        echo "<td>";
        echo htmlentities($string->surname, ENT_QUOTES, 'UTF-8');
        echo "</td><td>";
        echo htmlentities($string->lastname, ENT_QUOTES, 'UTF-8');
        echo "</td><td>";
        controllerClass::getImage($string->class);
        echo "</td><td>";
        echo self::getOrganisation($string->_oid);
        echo "</td><td>";
        controllerRace::getImage($string->race);
        self::getGender($string->gender);
        echo "</td><td>";
        echo $string->age;
        echo "</td><td>";
        echo htmlentities($string->status, ENT_QUOTES, 'UTF-8');
        echo "</td><td>";
        echo controllerProfession::get($string->profession)->name;
        echo "</td>";
    }

    public static function getAdminRow($string) {
        echo "<td>";
        echo htmlentities($string->surname, ENT_QUOTES, 'UTF-8');
        echo "</td><td>";
        echo htmlentities($string->lastname, ENT_QUOTES, 'UTF-8');
        echo "</td><td>";
        echo self::getCharacter($string->_cid);
        echo "</td><td>";
        echo self::getOrganisation($string->_oid);
        echo "</td><td>";
        echo "<a href='#' onclick='loadTabContent(\"./view/admin.character.edit.php\",\"" . $string->_id . "\")'><img title='" . htmlentities($string->surname, ENT_QUOTES, 'UTF-8') . " bearbeiten' src='./res/icons/pencil.png'></a>";
        echo "</td><td>";
        echo "<a href='#' onclick='removeCharacter(" . $string->_id . ")'><img title='" . htmlentities($string->surname, ENT_QUOTES, 'UTF-8') . " löschen' src='./res/icons/close.png'></a>";
        echo "</td>";
    }

    public static function getRoleplayRow($string) {
        echo "<td><span id=".$string->_id.">";
        echo htmlentities($string->surname, ENT_QUOTES, 'UTF-8');
        echo "</span></td><td><span id=".$string->_id.">";
        echo htmlentities($string->lastname, ENT_QUOTES, 'UTF-8');
        echo "</span></td><td>";
        echo self::getOrganisation($string->_oid);
        echo "</td><td>";
        echo self::getRank($string->_rid);
        echo "</td><td>";
        controllerRace::getImage($string->race);
        self::getGender($string->gender);
        echo "</td><td>";
        echo $string->age;
        echo "</td><td>";
        echo htmlentities($string->status, ENT_QUOTES, 'UTF-8');
        echo "</td><td>";
        echo self::getRelations($string->_id);
        echo "</td>";
    }

    public static function getGuildRow($string) {
        echo "<td><span id=".$string->_id.">";
        echo htmlentities($string->surname, ENT_QUOTES, 'UTF-8');
        echo "</span></td><td><span id=".$string->_id.">";
        echo htmlentities($string->lastname, ENT_QUOTES, 'UTF-8');
        echo "</span></td><td>";
        controllerClass::getImage($string->class);
        echo "</td><td>";
        echo self::getRank($string->_rid);
        echo "</td><td>";
        controllerRace::getImage($string->race);
        self::getGender($string->gender);
        echo "</td><td>";
        echo htmlentities($string->task, ENT_QUOTES, 'UTF-8');
        echo "</td><td>";
        echo self::getCharacter($string->_cid);
        echo "</td><td>";
        controllerProfession::get($string->profession);
        echo "</td>";
    }

    public static function getNscRow($string) {
        echo "<td><span id=".$string->_id.">";
        echo htmlentities($string->surname, ENT_QUOTES, 'UTF-8');
        echo "</span></td><td><span id=".$string->_id.">";
        echo htmlentities($string->lastname, ENT_QUOTES, 'UTF-8');
        echo "</span></td><td>";
        echo self::getOrganisation($string->_oid);
        echo "</td><td>";
        echo $string->titel;
        echo "</td><td>";
        controllerRace::getImage($string->race);
        self::getGender($string->gender);
        echo "</td><td>";
        echo $string->age;
        echo "</td><td>";
        echo htmlentities($string->status, ENT_QUOTES, 'UTF-8');
        echo "</td><td>";
        echo htmlentities($string->description, ENT_QUOTES, 'UTF-8');
        echo "</td>";
    }

}

?>
